{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Looking in indexes: https://mirrors.sjtug.sjtu.edu.cn/pypi/web/simple\n",
      "Collecting graphviz\n",
      "  Downloading https://mirror.sjtu.edu.cn/pypi-packages/86/86/89ba50ba65928001d3161f23bfa03945ed18ea13a1d1d44a772ff1fa4e7a/graphviz-0.16-py2.py3-none-any.whl (19 kB)\n",
      "Installing collected packages: graphviz\n",
      "Successfully installed graphviz-0.16\n",
      "Note: you may need to restart the kernel to use updated packages.\n"
     ]
    }
   ],
   "source": [
    "pip install graphviz"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 决策树"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn import tree #导入树\n",
    "from sklearn.tree import DecisionTreeClassifier #导入决策树分类器\n",
    "from sklearn.datasets import load_iris #导入鸢尾花数据集\n",
    "from sklearn.model_selection import train_test_split #分训练集测试集的类\n",
    "from matplotlib import pyplot as plt #画图用的\n",
    "from sklearn.model_selection import GridSearchCV #网格搜索\n",
    "import pandas as pd \n",
    "import graphviz #画决策树的，需要事先安装"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "iris = load_iris() #将数据集实例化，别忘了括号\n",
    "Xtrain,Xtest,Ytrain,Ytest = train_test_split(iris.data,iris.target,test_size=0.2) #将鸢尾花数据的特征矩阵和标签矩阵，按7:3的比例划分训练集和测试集，0.3是可以变的，也可以填0.4，注意X，Y的顺序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9333333333333333"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf = DecisionTreeClassifier() #实例化模型，括号不填criterion默认是‘gini’,也可以填criterion = 'entropy'\n",
    "clf = clf.fit(Xtrain, Ytrain) #训练数据集\n",
    "score = clf.score(Xtest, Ytest) #评估数据集\n",
    "\n",
    "score #将评估结果打印出来，因为测试集和训练集划分的不同，可能每个人的结果也不同"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\r\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\r\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\r\n",
       "<!-- Generated by graphviz version 2.47.3 (20210619.1520)\r\n",
       " -->\r\n",
       "<!-- Title: Tree Pages: 1 -->\r\n",
       "<svg width=\"562pt\" height=\"552pt\"\r\n",
       " viewBox=\"0.00 0.00 562.00 552.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\r\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 548)\">\r\n",
       "<title>Tree</title>\r\n",
       "<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-548 558,-548 558,4 -4,4\"/>\r\n",
       "<!-- 0 -->\r\n",
       "<g id=\"node1\" class=\"node\">\r\n",
       "<title>0</title>\r\n",
       "<path fill=\"#ffffff\" stroke=\"black\" d=\"M262,-544C262,-544 149,-544 149,-544 143,-544 137,-538 137,-532 137,-532 137,-473 137,-473 137,-467 143,-461 149,-461 149,-461 262,-461 262,-461 268,-461 274,-467 274,-473 274,-473 274,-532 274,-532 274,-538 268,-544 262,-544\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"205.5\" y=\"-528.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">花瓣长度 &lt;= 2.6</text>\r\n",
       "<text text-anchor=\"middle\" x=\"205.5\" y=\"-513.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.666</text>\r\n",
       "<text text-anchor=\"middle\" x=\"205.5\" y=\"-498.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 120</text>\r\n",
       "<text text-anchor=\"middle\" x=\"205.5\" y=\"-483.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [41, 38, 41]</text>\r\n",
       "<text text-anchor=\"middle\" x=\"205.5\" y=\"-468.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = 清风藤</text>\r\n",
       "</g>\r\n",
       "<!-- 1 -->\r\n",
       "<g id=\"node2\" class=\"node\">\r\n",
       "<title>1</title>\r\n",
       "<path fill=\"#e58139\" stroke=\"black\" d=\"M182,-417.5C182,-417.5 85,-417.5 85,-417.5 79,-417.5 73,-411.5 73,-405.5 73,-405.5 73,-361.5 73,-361.5 73,-355.5 79,-349.5 85,-349.5 85,-349.5 182,-349.5 182,-349.5 188,-349.5 194,-355.5 194,-361.5 194,-361.5 194,-405.5 194,-405.5 194,-411.5 188,-417.5 182,-417.5\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"133.5\" y=\"-402.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.0</text>\r\n",
       "<text text-anchor=\"middle\" x=\"133.5\" y=\"-387.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 41</text>\r\n",
       "<text text-anchor=\"middle\" x=\"133.5\" y=\"-372.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [41, 0, 0]</text>\r\n",
       "<text text-anchor=\"middle\" x=\"133.5\" y=\"-357.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = 清风藤</text>\r\n",
       "</g>\r\n",
       "<!-- 0&#45;&gt;1 -->\r\n",
       "<g id=\"edge1\" class=\"edge\">\r\n",
       "<title>0&#45;&gt;1</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M180.52,-460.91C173.66,-449.76 166.21,-437.66 159.31,-426.44\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"162.13,-424.35 153.91,-417.67 156.17,-428.02 162.13,-424.35\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"148.12\" y=\"-438.29\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">True</text>\r\n",
       "</g>\r\n",
       "<!-- 2 -->\r\n",
       "<g id=\"node3\" class=\"node\">\r\n",
       "<title>2</title>\r\n",
       "<path fill=\"#f6f1fd\" stroke=\"black\" d=\"M332.5,-425C332.5,-425 224.5,-425 224.5,-425 218.5,-425 212.5,-419 212.5,-413 212.5,-413 212.5,-354 212.5,-354 212.5,-348 218.5,-342 224.5,-342 224.5,-342 332.5,-342 332.5,-342 338.5,-342 344.5,-348 344.5,-354 344.5,-354 344.5,-413 344.5,-413 344.5,-419 338.5,-425 332.5,-425\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"278.5\" y=\"-409.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">花瓣宽度 &lt;= 1.65</text>\r\n",
       "<text text-anchor=\"middle\" x=\"278.5\" y=\"-394.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.499</text>\r\n",
       "<text text-anchor=\"middle\" x=\"278.5\" y=\"-379.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 79</text>\r\n",
       "<text text-anchor=\"middle\" x=\"278.5\" y=\"-364.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [0, 38, 41]</text>\r\n",
       "<text text-anchor=\"middle\" x=\"278.5\" y=\"-349.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = 锦葵</text>\r\n",
       "</g>\r\n",
       "<!-- 0&#45;&gt;2 -->\r\n",
       "<g id=\"edge2\" class=\"edge\">\r\n",
       "<title>0&#45;&gt;2</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M230.83,-460.91C236.32,-452.1 242.19,-442.7 247.86,-433.61\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"250.89,-435.36 253.22,-425.02 244.95,-431.65 250.89,-435.36\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"258.86\" y=\"-445.68\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">False</text>\r\n",
       "</g>\r\n",
       "<!-- 3 -->\r\n",
       "<g id=\"node4\" class=\"node\">\r\n",
       "<title>3</title>\r\n",
       "<path fill=\"#44e688\" stroke=\"black\" d=\"M257.5,-306C257.5,-306 149.5,-306 149.5,-306 143.5,-306 137.5,-300 137.5,-294 137.5,-294 137.5,-235 137.5,-235 137.5,-229 143.5,-223 149.5,-223 149.5,-223 257.5,-223 257.5,-223 263.5,-223 269.5,-229 269.5,-235 269.5,-235 269.5,-294 269.5,-294 269.5,-300 263.5,-306 257.5,-306\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"203.5\" y=\"-290.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">花瓣长度 &lt;= 4.95</text>\r\n",
       "<text text-anchor=\"middle\" x=\"203.5\" y=\"-275.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.097</text>\r\n",
       "<text text-anchor=\"middle\" x=\"203.5\" y=\"-260.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 39</text>\r\n",
       "<text text-anchor=\"middle\" x=\"203.5\" y=\"-245.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [0, 37, 2]</text>\r\n",
       "<text text-anchor=\"middle\" x=\"203.5\" y=\"-230.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = 云芝</text>\r\n",
       "</g>\r\n",
       "<!-- 2&#45;&gt;3 -->\r\n",
       "<g id=\"edge3\" class=\"edge\">\r\n",
       "<title>2&#45;&gt;3</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M252.48,-341.91C246.84,-333.1 240.81,-323.7 234.98,-314.61\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"237.82,-312.55 229.47,-306.02 231.93,-316.33 237.82,-312.55\"/>\r\n",
       "</g>\r\n",
       "<!-- 8 -->\r\n",
       "<g id=\"node9\" class=\"node\">\r\n",
       "<title>8</title>\r\n",
       "<path fill=\"#843ee6\" stroke=\"black\" d=\"M407.5,-306C407.5,-306 299.5,-306 299.5,-306 293.5,-306 287.5,-300 287.5,-294 287.5,-294 287.5,-235 287.5,-235 287.5,-229 293.5,-223 299.5,-223 299.5,-223 407.5,-223 407.5,-223 413.5,-223 419.5,-229 419.5,-235 419.5,-235 419.5,-294 419.5,-294 419.5,-300 413.5,-306 407.5,-306\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"353.5\" y=\"-290.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">花瓣长度 &lt;= 4.85</text>\r\n",
       "<text text-anchor=\"middle\" x=\"353.5\" y=\"-275.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.049</text>\r\n",
       "<text text-anchor=\"middle\" x=\"353.5\" y=\"-260.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 40</text>\r\n",
       "<text text-anchor=\"middle\" x=\"353.5\" y=\"-245.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [0, 1, 39]</text>\r\n",
       "<text text-anchor=\"middle\" x=\"353.5\" y=\"-230.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = 锦葵</text>\r\n",
       "</g>\r\n",
       "<!-- 2&#45;&gt;8 -->\r\n",
       "<g id=\"edge8\" class=\"edge\">\r\n",
       "<title>2&#45;&gt;8</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M304.52,-341.91C310.16,-333.1 316.19,-323.7 322.02,-314.61\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"325.07,-316.33 327.53,-306.02 319.18,-312.55 325.07,-316.33\"/>\r\n",
       "</g>\r\n",
       "<!-- 4 -->\r\n",
       "<g id=\"node5\" class=\"node\">\r\n",
       "<title>4</title>\r\n",
       "<path fill=\"#39e581\" stroke=\"black\" d=\"M109,-179.5C109,-179.5 12,-179.5 12,-179.5 6,-179.5 0,-173.5 0,-167.5 0,-167.5 0,-123.5 0,-123.5 0,-117.5 6,-111.5 12,-111.5 12,-111.5 109,-111.5 109,-111.5 115,-111.5 121,-117.5 121,-123.5 121,-123.5 121,-167.5 121,-167.5 121,-173.5 115,-179.5 109,-179.5\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"60.5\" y=\"-164.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.0</text>\r\n",
       "<text text-anchor=\"middle\" x=\"60.5\" y=\"-149.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 36</text>\r\n",
       "<text text-anchor=\"middle\" x=\"60.5\" y=\"-134.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [0, 36, 0]</text>\r\n",
       "<text text-anchor=\"middle\" x=\"60.5\" y=\"-119.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = 云芝</text>\r\n",
       "</g>\r\n",
       "<!-- 3&#45;&gt;4 -->\r\n",
       "<g id=\"edge4\" class=\"edge\">\r\n",
       "<title>3&#45;&gt;4</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M153.89,-222.91C139.32,-210.99 123.41,-197.98 108.92,-186.12\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"110.99,-183.29 101.04,-179.67 106.56,-188.71 110.99,-183.29\"/>\r\n",
       "</g>\r\n",
       "<!-- 5 -->\r\n",
       "<g id=\"node6\" class=\"node\">\r\n",
       "<title>5</title>\r\n",
       "<path fill=\"#c09cf2\" stroke=\"black\" d=\"M259.5,-187C259.5,-187 151.5,-187 151.5,-187 145.5,-187 139.5,-181 139.5,-175 139.5,-175 139.5,-116 139.5,-116 139.5,-110 145.5,-104 151.5,-104 151.5,-104 259.5,-104 259.5,-104 265.5,-104 271.5,-110 271.5,-116 271.5,-116 271.5,-175 271.5,-175 271.5,-181 265.5,-187 259.5,-187\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"205.5\" y=\"-171.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">花瓣宽度 &lt;= 1.55</text>\r\n",
       "<text text-anchor=\"middle\" x=\"205.5\" y=\"-156.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.444</text>\r\n",
       "<text text-anchor=\"middle\" x=\"205.5\" y=\"-141.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 3</text>\r\n",
       "<text text-anchor=\"middle\" x=\"205.5\" y=\"-126.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [0, 1, 2]</text>\r\n",
       "<text text-anchor=\"middle\" x=\"205.5\" y=\"-111.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = 锦葵</text>\r\n",
       "</g>\r\n",
       "<!-- 3&#45;&gt;5 -->\r\n",
       "<g id=\"edge5\" class=\"edge\">\r\n",
       "<title>3&#45;&gt;5</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M204.19,-222.91C204.34,-214.56 204.49,-205.67 204.64,-197.02\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"208.14,-197.08 204.81,-187.02 201.14,-196.96 208.14,-197.08\"/>\r\n",
       "</g>\r\n",
       "<!-- 6 -->\r\n",
       "<g id=\"node7\" class=\"node\">\r\n",
       "<title>6</title>\r\n",
       "<path fill=\"#8139e5\" stroke=\"black\" d=\"M123,-68C123,-68 34,-68 34,-68 28,-68 22,-62 22,-56 22,-56 22,-12 22,-12 22,-6 28,0 34,0 34,0 123,0 123,0 129,0 135,-6 135,-12 135,-12 135,-56 135,-56 135,-62 129,-68 123,-68\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"78.5\" y=\"-52.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.0</text>\r\n",
       "<text text-anchor=\"middle\" x=\"78.5\" y=\"-37.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 2</text>\r\n",
       "<text text-anchor=\"middle\" x=\"78.5\" y=\"-22.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [0, 0, 2]</text>\r\n",
       "<text text-anchor=\"middle\" x=\"78.5\" y=\"-7.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = 锦葵</text>\r\n",
       "</g>\r\n",
       "<!-- 5&#45;&gt;6 -->\r\n",
       "<g id=\"edge6\" class=\"edge\">\r\n",
       "<title>5&#45;&gt;6</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M158.21,-103.73C147.31,-94.33 135.74,-84.35 124.88,-74.99\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"126.98,-72.18 117.12,-68.3 122.41,-77.48 126.98,-72.18\"/>\r\n",
       "</g>\r\n",
       "<!-- 7 -->\r\n",
       "<g id=\"node8\" class=\"node\">\r\n",
       "<title>7</title>\r\n",
       "<path fill=\"#39e581\" stroke=\"black\" d=\"M254,-68C254,-68 165,-68 165,-68 159,-68 153,-62 153,-56 153,-56 153,-12 153,-12 153,-6 159,0 165,0 165,0 254,0 254,0 260,0 266,-6 266,-12 266,-12 266,-56 266,-56 266,-62 260,-68 254,-68\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"209.5\" y=\"-52.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.0</text>\r\n",
       "<text text-anchor=\"middle\" x=\"209.5\" y=\"-37.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 1</text>\r\n",
       "<text text-anchor=\"middle\" x=\"209.5\" y=\"-22.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [0, 1, 0]</text>\r\n",
       "<text text-anchor=\"middle\" x=\"209.5\" y=\"-7.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = 云芝</text>\r\n",
       "</g>\r\n",
       "<!-- 5&#45;&gt;7 -->\r\n",
       "<g id=\"edge7\" class=\"edge\">\r\n",
       "<title>5&#45;&gt;7</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M206.99,-103.73C207.29,-95.52 207.61,-86.86 207.91,-78.56\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"211.42,-78.42 208.28,-68.3 204.42,-78.17 211.42,-78.42\"/>\r\n",
       "</g>\r\n",
       "<!-- 9 -->\r\n",
       "<g id=\"node10\" class=\"node\">\r\n",
       "<title>9</title>\r\n",
       "<path fill=\"#c09cf2\" stroke=\"black\" d=\"M402.5,-187C402.5,-187 302.5,-187 302.5,-187 296.5,-187 290.5,-181 290.5,-175 290.5,-175 290.5,-116 290.5,-116 290.5,-110 296.5,-104 302.5,-104 302.5,-104 402.5,-104 402.5,-104 408.5,-104 414.5,-110 414.5,-116 414.5,-116 414.5,-175 414.5,-175 414.5,-181 408.5,-187 402.5,-187\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"352.5\" y=\"-171.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">花萼宽度 &lt;= 3.1</text>\r\n",
       "<text text-anchor=\"middle\" x=\"352.5\" y=\"-156.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.444</text>\r\n",
       "<text text-anchor=\"middle\" x=\"352.5\" y=\"-141.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 3</text>\r\n",
       "<text text-anchor=\"middle\" x=\"352.5\" y=\"-126.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [0, 1, 2]</text>\r\n",
       "<text text-anchor=\"middle\" x=\"352.5\" y=\"-111.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = 锦葵</text>\r\n",
       "</g>\r\n",
       "<!-- 8&#45;&gt;9 -->\r\n",
       "<g id=\"edge9\" class=\"edge\">\r\n",
       "<title>8&#45;&gt;9</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M353.15,-222.91C353.08,-214.65 353.01,-205.86 352.93,-197.3\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"356.43,-196.99 352.85,-187.02 349.43,-197.05 356.43,-196.99\"/>\r\n",
       "</g>\r\n",
       "<!-- 12 -->\r\n",
       "<g id=\"node13\" class=\"node\">\r\n",
       "<title>12</title>\r\n",
       "<path fill=\"#8139e5\" stroke=\"black\" d=\"M542,-179.5C542,-179.5 445,-179.5 445,-179.5 439,-179.5 433,-173.5 433,-167.5 433,-167.5 433,-123.5 433,-123.5 433,-117.5 439,-111.5 445,-111.5 445,-111.5 542,-111.5 542,-111.5 548,-111.5 554,-117.5 554,-123.5 554,-123.5 554,-167.5 554,-167.5 554,-173.5 548,-179.5 542,-179.5\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"493.5\" y=\"-164.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.0</text>\r\n",
       "<text text-anchor=\"middle\" x=\"493.5\" y=\"-149.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 37</text>\r\n",
       "<text text-anchor=\"middle\" x=\"493.5\" y=\"-134.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [0, 0, 37]</text>\r\n",
       "<text text-anchor=\"middle\" x=\"493.5\" y=\"-119.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = 锦葵</text>\r\n",
       "</g>\r\n",
       "<!-- 8&#45;&gt;12 -->\r\n",
       "<g id=\"edge12\" class=\"edge\">\r\n",
       "<title>8&#45;&gt;12</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M402.07,-222.91C416.33,-210.99 431.91,-197.98 446.09,-186.12\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"448.38,-188.77 453.81,-179.67 443.9,-183.39 448.38,-188.77\"/>\r\n",
       "</g>\r\n",
       "<!-- 10 -->\r\n",
       "<g id=\"node11\" class=\"node\">\r\n",
       "<title>10</title>\r\n",
       "<path fill=\"#8139e5\" stroke=\"black\" d=\"M394,-68C394,-68 305,-68 305,-68 299,-68 293,-62 293,-56 293,-56 293,-12 293,-12 293,-6 299,0 305,0 305,0 394,0 394,0 400,0 406,-6 406,-12 406,-12 406,-56 406,-56 406,-62 400,-68 394,-68\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"349.5\" y=\"-52.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.0</text>\r\n",
       "<text text-anchor=\"middle\" x=\"349.5\" y=\"-37.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 2</text>\r\n",
       "<text text-anchor=\"middle\" x=\"349.5\" y=\"-22.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [0, 0, 2]</text>\r\n",
       "<text text-anchor=\"middle\" x=\"349.5\" y=\"-7.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = 锦葵</text>\r\n",
       "</g>\r\n",
       "<!-- 9&#45;&gt;10 -->\r\n",
       "<g id=\"edge10\" class=\"edge\">\r\n",
       "<title>9&#45;&gt;10</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M351.38,-103.73C351.16,-95.52 350.92,-86.86 350.69,-78.56\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"354.18,-78.2 350.41,-68.3 347.19,-78.39 354.18,-78.2\"/>\r\n",
       "</g>\r\n",
       "<!-- 11 -->\r\n",
       "<g id=\"node12\" class=\"node\">\r\n",
       "<title>11</title>\r\n",
       "<path fill=\"#39e581\" stroke=\"black\" d=\"M525,-68C525,-68 436,-68 436,-68 430,-68 424,-62 424,-56 424,-56 424,-12 424,-12 424,-6 430,0 436,0 436,0 525,0 525,0 531,0 537,-6 537,-12 537,-12 537,-56 537,-56 537,-62 531,-68 525,-68\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"480.5\" y=\"-52.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.0</text>\r\n",
       "<text text-anchor=\"middle\" x=\"480.5\" y=\"-37.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 1</text>\r\n",
       "<text text-anchor=\"middle\" x=\"480.5\" y=\"-22.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [0, 1, 0]</text>\r\n",
       "<text text-anchor=\"middle\" x=\"480.5\" y=\"-7.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = 云芝</text>\r\n",
       "</g>\r\n",
       "<!-- 9&#45;&gt;11 -->\r\n",
       "<g id=\"edge11\" class=\"edge\">\r\n",
       "<title>9&#45;&gt;11</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M400.16,-103.73C411.14,-94.33 422.81,-84.35 433.76,-74.99\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"436.25,-77.46 441.57,-68.3 431.7,-72.14 436.25,-77.46\"/>\r\n",
       "</g>\r\n",
       "</g>\r\n",
       "</svg>\r\n"
      ],
      "text/plain": [
       "<graphviz.files.Source at 0x28dff21dca0>"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "feature_name = ['花萼长度','花萼宽度','花瓣长度','花瓣宽度'] #定义特征的名字，方便后面的阅读\n",
    "dot_data = tree.export_graphviz(clf\n",
    "                                ,feature_names = feature_name\n",
    "                                ,class_names=[\"清风藤\",\"云芝\",\"锦葵\"] #标签名字\n",
    "                                ,filled=True #框框填充颜色（可以不写）\n",
    "                                ,rounded=True #框框角是圆圆的（可以不写）\n",
    "                               ) \n",
    "graph = graphviz.Source(dot_data) #导出树\n",
    "graph"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf = tree.DecisionTreeClassifier(max_depth=4  #最大深度是2，这个也可以填3或4试试看\n",
    "                                 ,min_samples_leaf=5 #下一个叶子节点大于5会进行，小于5就不会再分\n",
    "                                 ,min_samples_split=10 #最小分支节点，当前样本大于10才会分\n",
    "                                 ,max_features=3 #最大特征数，有一个重要程度为0\n",
    "                                    )\n",
    "clf = clf.fit(Xtrain, Ytrain)\n",
    "score = clf.score(Xtest, Ytest)\n",
    "\n",
    "score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\r\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\r\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\r\n",
       "<!-- Generated by graphviz version 2.47.3 (20210619.1520)\r\n",
       " -->\r\n",
       "<!-- Title: Tree Pages: 1 -->\r\n",
       "<svg width=\"554pt\" height=\"552pt\"\r\n",
       " viewBox=\"0.00 0.00 554.00 552.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\r\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 548)\">\r\n",
       "<title>Tree</title>\r\n",
       "<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-548 550,-548 550,4 -4,4\"/>\r\n",
       "<!-- 0 -->\r\n",
       "<g id=\"node1\" class=\"node\">\r\n",
       "<title>0</title>\r\n",
       "<path fill=\"#ffffff\" stroke=\"black\" d=\"M261,-544C261,-544 148,-544 148,-544 142,-544 136,-538 136,-532 136,-532 136,-473 136,-473 136,-467 142,-461 148,-461 148,-461 261,-461 261,-461 267,-461 273,-467 273,-473 273,-473 273,-532 273,-532 273,-538 267,-544 261,-544\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"204.5\" y=\"-528.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">花瓣宽度 &lt;= 0.8</text>\r\n",
       "<text text-anchor=\"middle\" x=\"204.5\" y=\"-513.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.666</text>\r\n",
       "<text text-anchor=\"middle\" x=\"204.5\" y=\"-498.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 120</text>\r\n",
       "<text text-anchor=\"middle\" x=\"204.5\" y=\"-483.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [41, 38, 41]</text>\r\n",
       "<text text-anchor=\"middle\" x=\"204.5\" y=\"-468.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = 清风藤</text>\r\n",
       "</g>\r\n",
       "<!-- 1 -->\r\n",
       "<g id=\"node2\" class=\"node\">\r\n",
       "<title>1</title>\r\n",
       "<path fill=\"#e58139\" stroke=\"black\" d=\"M181,-417.5C181,-417.5 84,-417.5 84,-417.5 78,-417.5 72,-411.5 72,-405.5 72,-405.5 72,-361.5 72,-361.5 72,-355.5 78,-349.5 84,-349.5 84,-349.5 181,-349.5 181,-349.5 187,-349.5 193,-355.5 193,-361.5 193,-361.5 193,-405.5 193,-405.5 193,-411.5 187,-417.5 181,-417.5\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"132.5\" y=\"-402.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.0</text>\r\n",
       "<text text-anchor=\"middle\" x=\"132.5\" y=\"-387.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 41</text>\r\n",
       "<text text-anchor=\"middle\" x=\"132.5\" y=\"-372.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [41, 0, 0]</text>\r\n",
       "<text text-anchor=\"middle\" x=\"132.5\" y=\"-357.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = 清风藤</text>\r\n",
       "</g>\r\n",
       "<!-- 0&#45;&gt;1 -->\r\n",
       "<g id=\"edge1\" class=\"edge\">\r\n",
       "<title>0&#45;&gt;1</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M179.52,-460.91C172.66,-449.76 165.21,-437.66 158.31,-426.44\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"161.13,-424.35 152.91,-417.67 155.17,-428.02 161.13,-424.35\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"147.12\" y=\"-438.29\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">True</text>\r\n",
       "</g>\r\n",
       "<!-- 2 -->\r\n",
       "<g id=\"node3\" class=\"node\">\r\n",
       "<title>2</title>\r\n",
       "<path fill=\"#f6f1fd\" stroke=\"black\" d=\"M331.5,-425C331.5,-425 223.5,-425 223.5,-425 217.5,-425 211.5,-419 211.5,-413 211.5,-413 211.5,-354 211.5,-354 211.5,-348 217.5,-342 223.5,-342 223.5,-342 331.5,-342 331.5,-342 337.5,-342 343.5,-348 343.5,-354 343.5,-354 343.5,-413 343.5,-413 343.5,-419 337.5,-425 331.5,-425\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"277.5\" y=\"-409.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">花瓣宽度 &lt;= 1.65</text>\r\n",
       "<text text-anchor=\"middle\" x=\"277.5\" y=\"-394.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.499</text>\r\n",
       "<text text-anchor=\"middle\" x=\"277.5\" y=\"-379.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 79</text>\r\n",
       "<text text-anchor=\"middle\" x=\"277.5\" y=\"-364.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [0, 38, 41]</text>\r\n",
       "<text text-anchor=\"middle\" x=\"277.5\" y=\"-349.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = 锦葵</text>\r\n",
       "</g>\r\n",
       "<!-- 0&#45;&gt;2 -->\r\n",
       "<g id=\"edge2\" class=\"edge\">\r\n",
       "<title>0&#45;&gt;2</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M229.83,-460.91C235.32,-452.1 241.19,-442.7 246.86,-433.61\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"249.89,-435.36 252.22,-425.02 243.95,-431.65 249.89,-435.36\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"257.86\" y=\"-445.68\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">False</text>\r\n",
       "</g>\r\n",
       "<!-- 3 -->\r\n",
       "<g id=\"node4\" class=\"node\">\r\n",
       "<title>3</title>\r\n",
       "<path fill=\"#44e688\" stroke=\"black\" d=\"M256.5,-306C256.5,-306 148.5,-306 148.5,-306 142.5,-306 136.5,-300 136.5,-294 136.5,-294 136.5,-235 136.5,-235 136.5,-229 142.5,-223 148.5,-223 148.5,-223 256.5,-223 256.5,-223 262.5,-223 268.5,-229 268.5,-235 268.5,-235 268.5,-294 268.5,-294 268.5,-300 262.5,-306 256.5,-306\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"202.5\" y=\"-290.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">花瓣宽度 &lt;= 1.45</text>\r\n",
       "<text text-anchor=\"middle\" x=\"202.5\" y=\"-275.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.097</text>\r\n",
       "<text text-anchor=\"middle\" x=\"202.5\" y=\"-260.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 39</text>\r\n",
       "<text text-anchor=\"middle\" x=\"202.5\" y=\"-245.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [0, 37, 2]</text>\r\n",
       "<text text-anchor=\"middle\" x=\"202.5\" y=\"-230.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = 云芝</text>\r\n",
       "</g>\r\n",
       "<!-- 2&#45;&gt;3 -->\r\n",
       "<g id=\"edge3\" class=\"edge\">\r\n",
       "<title>2&#45;&gt;3</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M251.48,-341.91C245.84,-333.1 239.81,-323.7 233.98,-314.61\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"236.82,-312.55 228.47,-306.02 230.93,-316.33 236.82,-312.55\"/>\r\n",
       "</g>\r\n",
       "<!-- 8 -->\r\n",
       "<g id=\"node9\" class=\"node\">\r\n",
       "<title>8</title>\r\n",
       "<path fill=\"#843ee6\" stroke=\"black\" d=\"M406.5,-306C406.5,-306 298.5,-306 298.5,-306 292.5,-306 286.5,-300 286.5,-294 286.5,-294 286.5,-235 286.5,-235 286.5,-229 292.5,-223 298.5,-223 298.5,-223 406.5,-223 406.5,-223 412.5,-223 418.5,-229 418.5,-235 418.5,-235 418.5,-294 418.5,-294 418.5,-300 412.5,-306 406.5,-306\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"352.5\" y=\"-290.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">花萼长度 &lt;= 5.95</text>\r\n",
       "<text text-anchor=\"middle\" x=\"352.5\" y=\"-275.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.049</text>\r\n",
       "<text text-anchor=\"middle\" x=\"352.5\" y=\"-260.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 40</text>\r\n",
       "<text text-anchor=\"middle\" x=\"352.5\" y=\"-245.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [0, 1, 39]</text>\r\n",
       "<text text-anchor=\"middle\" x=\"352.5\" y=\"-230.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = 锦葵</text>\r\n",
       "</g>\r\n",
       "<!-- 2&#45;&gt;8 -->\r\n",
       "<g id=\"edge8\" class=\"edge\">\r\n",
       "<title>2&#45;&gt;8</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M303.52,-341.91C309.16,-333.1 315.19,-323.7 321.02,-314.61\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"324.07,-316.33 326.53,-306.02 318.18,-312.55 324.07,-316.33\"/>\r\n",
       "</g>\r\n",
       "<!-- 4 -->\r\n",
       "<g id=\"node5\" class=\"node\">\r\n",
       "<title>4</title>\r\n",
       "<path fill=\"#39e581\" stroke=\"black\" d=\"M109,-179.5C109,-179.5 12,-179.5 12,-179.5 6,-179.5 0,-173.5 0,-167.5 0,-167.5 0,-123.5 0,-123.5 0,-117.5 6,-111.5 12,-111.5 12,-111.5 109,-111.5 109,-111.5 115,-111.5 121,-117.5 121,-123.5 121,-123.5 121,-167.5 121,-167.5 121,-173.5 115,-179.5 109,-179.5\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"60.5\" y=\"-164.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.0</text>\r\n",
       "<text text-anchor=\"middle\" x=\"60.5\" y=\"-149.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 26</text>\r\n",
       "<text text-anchor=\"middle\" x=\"60.5\" y=\"-134.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [0, 26, 0]</text>\r\n",
       "<text text-anchor=\"middle\" x=\"60.5\" y=\"-119.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = 云芝</text>\r\n",
       "</g>\r\n",
       "<!-- 3&#45;&gt;4 -->\r\n",
       "<g id=\"edge4\" class=\"edge\">\r\n",
       "<title>3&#45;&gt;4</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M153.23,-222.91C138.77,-210.99 122.97,-197.98 108.59,-186.12\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"110.7,-183.32 100.75,-179.67 106.25,-188.73 110.7,-183.32\"/>\r\n",
       "</g>\r\n",
       "<!-- 5 -->\r\n",
       "<g id=\"node6\" class=\"node\">\r\n",
       "<title>5</title>\r\n",
       "<path fill=\"#5dea98\" stroke=\"black\" d=\"M259.5,-187C259.5,-187 151.5,-187 151.5,-187 145.5,-187 139.5,-181 139.5,-175 139.5,-175 139.5,-116 139.5,-116 139.5,-110 145.5,-104 151.5,-104 151.5,-104 259.5,-104 259.5,-104 265.5,-104 271.5,-110 271.5,-116 271.5,-116 271.5,-175 271.5,-175 271.5,-181 265.5,-187 259.5,-187\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"205.5\" y=\"-171.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">花萼宽度 &lt;= 2.85</text>\r\n",
       "<text text-anchor=\"middle\" x=\"205.5\" y=\"-156.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.26</text>\r\n",
       "<text text-anchor=\"middle\" x=\"205.5\" y=\"-141.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 13</text>\r\n",
       "<text text-anchor=\"middle\" x=\"205.5\" y=\"-126.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [0, 11, 2]</text>\r\n",
       "<text text-anchor=\"middle\" x=\"205.5\" y=\"-111.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = 云芝</text>\r\n",
       "</g>\r\n",
       "<!-- 3&#45;&gt;5 -->\r\n",
       "<g id=\"edge5\" class=\"edge\">\r\n",
       "<title>3&#45;&gt;5</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M203.54,-222.91C203.75,-214.56 203.98,-205.67 204.2,-197.02\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"207.7,-197.11 204.46,-187.02 200.71,-196.93 207.7,-197.11\"/>\r\n",
       "</g>\r\n",
       "<!-- 6 -->\r\n",
       "<g id=\"node7\" class=\"node\">\r\n",
       "<title>6</title>\r\n",
       "<path fill=\"#bdf6d5\" stroke=\"black\" d=\"M185,-68C185,-68 96,-68 96,-68 90,-68 84,-62 84,-56 84,-56 84,-12 84,-12 84,-6 90,0 96,0 96,0 185,0 185,0 191,0 197,-6 197,-12 197,-12 197,-56 197,-56 197,-62 191,-68 185,-68\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"140.5\" y=\"-52.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.48</text>\r\n",
       "<text text-anchor=\"middle\" x=\"140.5\" y=\"-37.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 5</text>\r\n",
       "<text text-anchor=\"middle\" x=\"140.5\" y=\"-22.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [0, 3, 2]</text>\r\n",
       "<text text-anchor=\"middle\" x=\"140.5\" y=\"-7.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = 云芝</text>\r\n",
       "</g>\r\n",
       "<!-- 5&#45;&gt;6 -->\r\n",
       "<g id=\"edge6\" class=\"edge\">\r\n",
       "<title>5&#45;&gt;6</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M181.3,-103.73C176.1,-94.97 170.6,-85.7 165.38,-76.91\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"168.38,-75.11 160.27,-68.3 162.36,-78.69 168.38,-75.11\"/>\r\n",
       "</g>\r\n",
       "<!-- 7 -->\r\n",
       "<g id=\"node8\" class=\"node\">\r\n",
       "<title>7</title>\r\n",
       "<path fill=\"#39e581\" stroke=\"black\" d=\"M316,-68C316,-68 227,-68 227,-68 221,-68 215,-62 215,-56 215,-56 215,-12 215,-12 215,-6 221,0 227,0 227,0 316,0 316,0 322,0 328,-6 328,-12 328,-12 328,-56 328,-56 328,-62 322,-68 316,-68\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"271.5\" y=\"-52.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.0</text>\r\n",
       "<text text-anchor=\"middle\" x=\"271.5\" y=\"-37.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 8</text>\r\n",
       "<text text-anchor=\"middle\" x=\"271.5\" y=\"-22.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [0, 8, 0]</text>\r\n",
       "<text text-anchor=\"middle\" x=\"271.5\" y=\"-7.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = 云芝</text>\r\n",
       "</g>\r\n",
       "<!-- 5&#45;&gt;7 -->\r\n",
       "<g id=\"edge7\" class=\"edge\">\r\n",
       "<title>5&#45;&gt;7</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M230.08,-103.73C235.35,-94.97 240.94,-85.7 246.24,-76.91\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"249.26,-78.67 251.43,-68.3 243.27,-75.06 249.26,-78.67\"/>\r\n",
       "</g>\r\n",
       "<!-- 9 -->\r\n",
       "<g id=\"node10\" class=\"node\">\r\n",
       "<title>9</title>\r\n",
       "<path fill=\"#a06aec\" stroke=\"black\" d=\"M395,-179.5C395,-179.5 306,-179.5 306,-179.5 300,-179.5 294,-173.5 294,-167.5 294,-167.5 294,-123.5 294,-123.5 294,-117.5 300,-111.5 306,-111.5 306,-111.5 395,-111.5 395,-111.5 401,-111.5 407,-117.5 407,-123.5 407,-123.5 407,-167.5 407,-167.5 407,-173.5 401,-179.5 395,-179.5\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"350.5\" y=\"-164.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.32</text>\r\n",
       "<text text-anchor=\"middle\" x=\"350.5\" y=\"-149.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 5</text>\r\n",
       "<text text-anchor=\"middle\" x=\"350.5\" y=\"-134.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [0, 1, 4]</text>\r\n",
       "<text text-anchor=\"middle\" x=\"350.5\" y=\"-119.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = 锦葵</text>\r\n",
       "</g>\r\n",
       "<!-- 8&#45;&gt;9 -->\r\n",
       "<g id=\"edge9\" class=\"edge\">\r\n",
       "<title>8&#45;&gt;9</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M351.81,-222.91C351.62,-212.2 351.43,-200.62 351.24,-189.78\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"354.74,-189.61 351.07,-179.67 347.74,-189.73 354.74,-189.61\"/>\r\n",
       "</g>\r\n",
       "<!-- 10 -->\r\n",
       "<g id=\"node11\" class=\"node\">\r\n",
       "<title>10</title>\r\n",
       "<path fill=\"#8139e5\" stroke=\"black\" d=\"M534,-179.5C534,-179.5 437,-179.5 437,-179.5 431,-179.5 425,-173.5 425,-167.5 425,-167.5 425,-123.5 425,-123.5 425,-117.5 431,-111.5 437,-111.5 437,-111.5 534,-111.5 534,-111.5 540,-111.5 546,-117.5 546,-123.5 546,-123.5 546,-167.5 546,-167.5 546,-173.5 540,-179.5 534,-179.5\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"485.5\" y=\"-164.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.0</text>\r\n",
       "<text text-anchor=\"middle\" x=\"485.5\" y=\"-149.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 35</text>\r\n",
       "<text text-anchor=\"middle\" x=\"485.5\" y=\"-134.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [0, 0, 35]</text>\r\n",
       "<text text-anchor=\"middle\" x=\"485.5\" y=\"-119.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = 锦葵</text>\r\n",
       "</g>\r\n",
       "<!-- 8&#45;&gt;10 -->\r\n",
       "<g id=\"edge10\" class=\"edge\">\r\n",
       "<title>8&#45;&gt;10</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M398.64,-222.91C412.07,-211.1 426.71,-198.22 440.09,-186.45\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"442.6,-188.9 447.8,-179.67 437.98,-183.64 442.6,-188.9\"/>\r\n",
       "</g>\r\n",
       "</g>\r\n",
       "</svg>\r\n"
      ],
      "text/plain": [
       "<graphviz.files.Source at 0x28dff301fa0>"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dot_data = tree.export_graphviz(clf\n",
    "                               ,feature_names = feature_name\n",
    "                               ,class_names=[\"清风藤\",\"云芝\",\"锦葵\"]\n",
    "                               ,filled=True\n",
    "                               ,rounded=True\n",
    "                               ) \n",
    "graph = graphviz.Source(dot_data)\n",
    "graph"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYgAAAEHCAYAAAC0pdErAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8vihELAAAACXBIWXMAAAsTAAALEwEAmpwYAAAf90lEQVR4nO3de5xcZZ3n8c+XTiJJuEMSJAECQ7gENoRQk2EXRQSBoCALoxjQRRg1xgHEcVXwNjo647Ky6uhyCVlA0OGyIGSIghBFMCNeqjoXyI1ICJg0QdLIPcGEJL/545zGolOdru70qVOX7/v16lfXuTx1fgmkv32ep87zKCIwMzPrboe8CzAzs/rkgDAzs4ocEGZmVpEDwszMKnJAmJlZRYPyLmAg7bXXXjF27Ni8yzAzaxjz5s17LiJGVDrWVAExduxY2tvb8y7DzKxhSPpDT8fcxWRmZhU5IMzMrCIHhJmZVeSAMDOzihwQZmZWkQPCzMwqckCYmVlFDgh7s/Xr4Y47YN26vCsxs5w5IOwv/vxnOOMMOPtsOOgguOYaeP31vKsys5w4ICyxaROccw78/Ofw1a8mAfH3fw+HHQa33gpbtuRdoZnVmAPCkh/+f/d38O//Dt/7HnzlKzB3LtxzDwwfDueeC5MmwU9/Cl6B0KxlOCBaXQR88pPwwx/C178OF1+c7Jfg3e+GBQvg5pvhlVeS7eOPh1//OteS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24DPAF8gmYvquHQm1X8EvpG2+1fgIElnAt8HPh4R63u4xleAX6XvMRvYD0DSYcAHSGaMnQhsBj6YthlOMineJJK1Br4SET8C2oEPpov3vJae+1x63jVp3WYDouXnYrKW92RELAKQtIRkVa6QtAgYC+xKMj/VOJK1IQYDRMQWSecDjwLXRsTD27jGcSSTuRER90jqmrPnRJLV2UrpfE9D+cssnFuA/5++/jeSWUt70nVsXtd1zAaCA8Ja3Yay11vKtreQ/Pv4OvBgRJyZLub0UNn544BXqW5MoNKkZwJuiojP97N9l66aN+N/0zaA3MVktm27Ak+nr8/v2ilpV5KuqeOAPdPxgZ7MJe06knQq0LWg/APA+8qmjd5D0v7psR2Arvc8F/hV+voVYOft+POYVc0BYbZt3wT+l6SHgbay/d8Bro6I3wMfAS7v+kFfwT+RLE85n2RNiVUAEbEU+BLJkqaPkqxO9ta0zTrgcEnzgBOAr6X7bwRmdBukNsuEp/s2q0OSXo2InfKuw1qb7yDMzKwi30GYDRBJFwCXdNv9cERcmEc9ZtvLAWFmZhW5i8nMzCpyQJiZWUUOCDMzq8gBYWZmFf0nh5QzJKOt0jMAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "test=[] #定义一个列表放分数\n",
    "for i in range(10):       #10次循环map_depth\n",
    "    clf = tree.DecisionTreeClassifier(max_depth=i+1)  \n",
    "    clf = clf.fit(Xtrain, Ytrain)\n",
    "    score = clf.score(Xtest,Ytest)\n",
    "    test.append(score)\n",
    "plt.plot(range(1,11),test,color=\"red\") #横坐标是1-10，纵坐标是分数，颜色是红色\n",
    "plt.xticks(range(1,11))#横坐标是1-10\n",
    "plt.xlabel(\"max_depth\") #横坐标标签\n",
    "plt.ylabel(\"score\") #纵坐标标签\n",
    "plt.show() #展示画好的图（由图可知当map_depth为2时分数最高）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9583333333333334"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "parameters = {'criterion':(\"gini\",\"entropy\")\n",
    "              ,\"max_depth\":[*range(1,5)] #前面我们知道这个应该是2，所以我们给定范围1-5\n",
    "              ,'min_samples_split':[*range(5,40,5)]#最小分支节点以步长为5，在5-39循环\n",
    "             }  #定义我们要找的参数\n",
    "clf = tree.DecisionTreeClassifier()\n",
    "GS = GridSearchCV(clf, parameters, cv=10) #cv=10代表交叉验证10次\n",
    "GS.fit(Xtrain,Ytrain)\n",
    "\n",
    "GS.best_params_ #打印最好的参数\n",
    "GS.best_score_ #打印最好的分数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## K-邻近"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0]\n",
      "['setosa']\n",
      "0.9736842105263158\n",
      "0.9736842105263158\n"
     ]
    }
   ],
   "source": [
    "from sklearn.datasets import load_iris\n",
    "#从sklearn的datasets导入鸢尾花的数据\n",
    "from sklearn.model_selection import train_test_split\n",
    "#导入的train_test_split方法用来打乱数据的顺序，返回有训练数据和测试数据集。比例为4：1\n",
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "#导入k近邻分类模型的类\n",
    "import numpy as np\n",
    "\n",
    "X_new=np.array([[4,2.9,1,0.2]])\n",
    "# scikit-learn输入数据必须是二维数组，我们必须遵守这个规则\n",
    "\n",
    "knn=KNeighborsClassifier(n_neighbors=1)\n",
    "#实例化学习模型\n",
    "iris_dataset = load_iris()\n",
    "#得到数据集\n",
    "X_train, X_test, y_train, y_test = train_test_split(\n",
    " iris_dataset['data'], iris_dataset['target'], random_state=0)\n",
    "#利用train_test_split得到训练数据和测试数据集，X表示样本输入，y表示样本的标签\n",
    "knn.fit(X_train,y_train)\n",
    "#就这么简单的一句即可完成模型的训练，fit方法回对knn进行修改返回\n",
    "predition =knn.predict(X_new)\n",
    "#利用模型进行预测\n",
    "print(predition)\n",
    "# 打印出类别\n",
    "\n",
    "print(iris_dataset['target_names'][predition])\n",
    "# 完整的打印出类别字符串\n",
    "\n",
    "# ----------开始评估模型是否准确\n",
    "y_pred=knn.predict(X_test)\n",
    "# 返回的是测试数据的预测结果\n",
    "\n",
    "print(np.mean(y_pred==y_test))\n",
    "# 打印出预测精度\n",
    "\n",
    "print(knn.score(X_test,y_test))\n",
    "# 用knn的score方法计算精度，与上面的方法是一样的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
